package operation.modules.workReport.controller;

import operation.modules.workReport.entity.WorkProjectEntity;
import operation.modules.workReport.entity.WorkProjectEntity;
import operation.modules.workReport.service.WorkProjectService;
import operation.util.ReqResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 项目 控制层
*/
@RestController
@RequestMapping("api/work/project")
public class WorkProjectController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WorkProjectService workProjectService;
    /**
     * 查询所有
     */
    @RequestMapping("/query-all")
    public ReqResult queryAll() {
        logger.info("项目表 查询所有数据");
        List<WorkProjectEntity> list = workProjectService.queryAll();
        return ReqResult.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ReqResult save(@RequestBody WorkProjectEntity workProjectEntity) {
        logger.info("项目表 保存信息");
        if (workProjectEntity.getTitle().equals("添加项目信息")) {
            workProjectEntity.setCreateTime(new Date());
            workProjectService.save(workProjectEntity);
        } else {
            workProjectService.updateById(workProjectEntity);
        }
        return ReqResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Map<String, Object> params) {
        logger.info("项目表 根据id删除");
        int id = (Integer) params.get("id");
        workProjectService.removeById(id);
        return ReqResult.ok();
    }
}